
import time


class Timer:
    def __init__(self):
        self.records = []
        self.duration = 0
        self.start = 0


    def Start(self):
        self.start = time.time()

    def Pause(self):
        self.duration += time.time() - self.start

    def StopAndRecord(self, info):
        self.duration += time.time() - self.start
        self.records.append((info, self.duration))
        self.duration = 0
    
    def StopAndPrint(self, info):
        self.duration += time.time() - self.start
        print("%s: %f"%(info, self.duration))
        self.duration = 0
    
    def PrintAll(self):
        for r in self.records:
            print("%s: %f"%(r[0], r[1]))

    def Save(self, save_file):
        f = open(save_file, "w")
        for r in self.records:
            f.write("%s: %f\n"%(r[0], r[1]))
